! VSemiletov, 08.2011
subroutine OutputTransducerAsynData()
use Data_cfoam, only: rank, P, U,T, Rho, ZeroEpsilon, Pnew, Unew,Tnew
use DataTime, only: nstep, ttime, dt=>dt2
use Data_Mesh, only: nCells, meshCellCenters !, nFaces
use DataCells, only: fluxesCellPresent, CellGlobalStepDone
use DataTime, only: nStepLocal, TimeLocal, TimeCell
use Data_Transducer
implicit none
!include 'mpif.h'

    integer :: iTrans, iCell, iCellOut
    real(8) :: alfa, beta
    
    real(8) :: timeCurrent,timeFormer, timeOut,timeOutFormer, timeAverage
    integer :: OpenFile
    real(8),allocatable :: Send(:)
    
    do iTrans = 1,nTransducers

!        if(Transducer(iTrans)%ndTimeOut.eq.1)then
!            open(1,file=Transducer(iTrans)%fileOutput(1:Transducer(iTrans)%nameLength),form='unformatted',IOSTAT=OpenFile)
!            if (OpenFile.ne.0) then
!                write(*,*) '@@ Fatal Error: File open error ', Transducer(iTrans)%fileOutput(1:Transducer(iTrans)%nameLength)
!                STOP '@@ Fatal Error!'
!            end if
!        else

!        end if

        if(Transducer(iTrans)%TableOut)then
            
                if(Transducer(iTrans)%EveryStep)then ! for tableOut only
                    beta = 1d0
                    TimeOut = ttime
                end if

                iCell = Transducer(iTrans)%CellsOut(1)
                if(fluxesCellPresent(iCell).eqv..false..or.CellGlobalStepDone(iCell)) cycle
                
                if(rank.eq.0)then
                    open(1,file=Transducer(iTrans)%fileOutput,position='append')
!                    write(1,'(f25.15)',ADVANCE = 'NO') TimeOut ! \)',ADVANCE = 'NO') TimeOut
                    write(1,'(f25.15)',ADVANCE = 'NO') TimeCell(iCell)+ttime
                    write(1,'(a3)',ADVANCE = 'NO') ' | ' ! \)',ADVANCE = 'NO') ' | '
                end if

                alfa = beta ! (TimeGlobal - TimeOut) / Transducer(iTrans)%dTimeOut

                allocate(Send(nCells))
                if(Transducer(iTrans)%Pres)then
                    call OutputScalarTransducer(pnew,p,iTrans,Send,alfa)
                end if
                if(Transducer(iTrans)%Uvel)then
                    call OutputVectorTransducer(Unew,U,iTrans,Send,alfa,1)
                end if
                if(Transducer(iTrans)%Vvel)then
                    call OutputVectorTransducer(Unew,U,iTrans,Send,alfa,2)
                end if
                if(Transducer(iTrans)%Wvel)then
                    call OutputVectorTransducer(Unew,U,iTrans,Send,alfa,3)
                end if
                if(Transducer(iTrans)%Temp)then
                    call OutputScalarTransducer(Tnew,T,iTrans,Send,alfa)
                end if

                if(rank.eq.0)then
                    write(1,'(a1)') ' '
    !                write(1,*) ' ' ! for desk top VSemiletov
                    close(1)
                end if

                deallocate(Send)

            cycle
        end if
        
        open(1,file=Transducer(iTrans)%fileOutput(1:Transducer(iTrans)%nameLength), &
                form='unformatted',position='append',IOSTAT=OpenFile)
        if (OpenFile.ne.0) then
            write(*,*) '@@ Fatal Error: File open error ', &
                    Transducer(iTrans)%fileOutput(1:Transducer(iTrans)%nameLength)
            STOP '@@ Fatal Error!'
        end if

        if(Transducer(iTrans)%EveryStep)then
            do iCellOut = 1,Transducer(iTrans)%nCellsOut
                iCell = Transducer(iTrans)%CellsOut(iCellOut)
                if(fluxesCellPresent(iCell).eqv..false..or.CellGlobalStepDone(iCell)) cycle
                
                Transducer(iTrans)%ndTimeOut = Transducer(iTrans)%ndTimeOut + 1
                if(TimeCell(iCell)+ttime-Transducer(iTrans)%TimeStart.lt.ZeroEpsilon.or. &
                   TimeCell(iCell)+ttime.gt.Transducer(iTrans)%TimeEnd) cycle

!                if(TimeCell(iCell)-Transducer(iTrans)%TimeStart.lt.ZeroEpsilon.or. &
!                   TimeCell(iCell).gt.Transducer(iTrans)%TimeEnd) cycle           
!write(*,*) 'I am here', TimeCell(iCell)+ttime-Transducer(iTrans)%TimeStart
                write(1) iCellOut, TimeCell(iCell)+ttime, &
                         U(1:3,iCell), p(iCell), Rho(iCell), TimeLocal+ttime
!                stop 'step done'
!                if(iCellOut.eq.1191)then
!                    write(197,'(6f20.10)') TimeCell(iCell)+ttime, p(iCell), U(1:3,iCell), Rho(iCell)
!                end if
!                    write(iCellOut+500,'(9f20.10)') TimeCell(iCell)+ttime, p(iCell), U(1:3,iCell), Rho(iCell), meshCellCenters(1:3,iCell)
            end do

            cycle
        end if
        
        close(1)
    end do

end subroutine OutputTransducerAsynData
